package com.ampaiva.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.ampaiva.R;
import com.ampaiva.app.CustomListActivity;
import com.ampaiva.db.DatabaseInfo;
import com.ampaiva.db.SQLiteAdapter;
import com.ampaiva.db.TableInfo;

public class TableList extends CustomListActivity<TableInfo> {
    public TableList() {
        this(R.layout.class, R.id.class);
    }

    public TableList(Class<?> Rlayoutclass, Class<?> Ridclass) {
        super(TableInfo.class, Rlayoutclass, Ridclass);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected List<TableInfo> getList() {
        SQLiteAdapter db = null;
        try {
            db = SQLiteAdapter.getDBAdapterInstance(this);
            db.open();
            DatabaseInfo dbInfo = db.getDabaseInfo();
            return dbInfo.getTablesInfo();
        } finally {
            if (db != null)
                db.close();
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        TableInfo selectedValue = (TableInfo) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue.toString(), Toast.LENGTH_SHORT).show();

        Intent i = new Intent(this, RecordList.class);
        i.putExtra("Table", selectedValue.getName());
        startActivity(i);
    }
}
